package de.android.client;

import messages.*;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


import messageprocessors.Appclient;

public class XmppAndroid extends Activity implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private EditText login_user_text;
	private EditText login_user_pw;
	private Spinner nutzung;
	
	/** Called when the activity is first created. */
	Appclient client;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nutzung=(Spinner) findViewById(R.id.spinn_type);
        login_user_text = (EditText) findViewById(R.id.login_user_text);
        login_user_pw = (EditText) findViewById(R.id.login_user_pw);
        
        
        
    }
	
	public void einloggen(View view)throws Exception{
		String username = login_user_text.getText().toString();
		String userpw=login_user_pw.getText().toString();
		client=new Appclient(username,userpw);
		 try {
     		 client.connect();
     		 //Toast.makeText(this,""+client.connection.isConnected()+""+client.connection.isAuthenticated(), Toast.LENGTH_SHORT).show();
     		 
     		 if (client.connection.isAuthenticated()==true){
     			client.disconnect();
     			int auswahl=nutzung.getSelectedItemPosition();
     				if (auswahl==0){
     					Intent myIntent = new Intent(view.getContext(), XmppAndroidInfo.class);
     					myIntent.putExtra("clientid", username);
     					myIntent.putExtra("clientpw", userpw);
     					startActivity(myIntent);
     				}else if (auswahl==1){
     					Intent myIntent2 = new Intent(view.getContext(), XmppAndroidWaren.class);
     					myIntent2.putExtra("clientid", username);
     					myIntent2.putExtra("clientpw", userpw);
     					startActivity(myIntent2);
     				}
     		 }else if (client.connection.isAuthenticated()==false){
     			 Toast.makeText(this, "Fehlerhafte Einloggdaten", Toast.LENGTH_SHORT).show();
     		 }
     		      	           
    	 } catch (Exception ex) {
    	//	 Log.e("HelloWorld", "Error in activity", ex);  // log the error
    		    // Also let the user know something went wrong
    	//	 Toast.makeText(getApplicationContext(), "Einloggen fehlgeschlagen", Toast.LENGTH_SHORT).show();
//    		    Toast.makeText(
//    		        getApplicationContext(),
//    		        ex.getClass().getName() + " " + ex.getMessage(),
//    		        Toast.LENGTH_LONG).show();
    	 }
    	
		
		
	}

}